package com.ruoyi.web.controller.fs;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.fs.domain.FsCapital;
import com.ruoyi.fs.domain.FsEntrust;
import com.ruoyi.fs.service.IFsCapitalService;
import com.ruoyi.fs.service.IFsEntrustService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsBatch;
import com.ruoyi.fs.service.IFsBatchService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 案件批次Controller
 *
 * @author hero
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/fs/batch")
public class FsBatchController extends BaseController {
    @Autowired
    private IFsBatchService fsBatchService;
    @Autowired
    private IFsCapitalService fsCapitalService;
    @Autowired
    private IFsEntrustService fsEntrustService;

    /**
     * 查询案件批次列表
     */
    @PreAuthorize("@ss.hasPermi('fs:batch:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsBatch fsBatch) {
        startPage();
        List<FsBatch> list = fsBatchService.selectFsBatchList(fsBatch);
        for (int i = 0; i < list.size(); i++) {
            FsBatch batch = list.get(i);
            Long zfId = batch.getZfid();
            FsEntrust entrust = fsEntrustService.selectFsEntrustById(zfId);
            if (entrust != null) {
                batch.setZfName(entrust.getName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询资方信息列表
     */
    @PreAuthorize("@ss.hasPermi('fs:capital:list')")
    @GetMapping("/listZf")
    public AjaxResult listZf() {
        FsEntrust entrust = new FsEntrust();
        List<FsEntrust> list = fsEntrustService.selectFsEntrustList(entrust);
        return success(list);
    }

    /**
     * 导出案件批次列表
     */
    @PreAuthorize("@ss.hasPermi('fs:batch:export')")
    @Log(title = "案件批次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsBatch fsBatch) {
        List<FsBatch> list = fsBatchService.selectFsBatchList(fsBatch);
        ExcelUtil<FsBatch> util = new ExcelUtil<FsBatch>(FsBatch.class);
        util.exportExcel(response, list, "案件批次数据");
    }

    /**
     * 获取案件批次详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:batch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fsBatchService.selectFsBatchById(id));
    }

    /**
     * 新增案件批次
     */
    @PreAuthorize("@ss.hasPermi('fs:batch:add')")
    @Log(title = "案件批次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsBatch fsBatch) {
        FsEntrust entrust = fsEntrustService.selectFsEntrustById(fsBatch.getZfid());
        fsBatch.setZfName(entrust.getName());
        fsBatch.setWtze(BigDecimal.ZERO);
        fsBatch.setCaseCount(0);
        fsBatchService.insertFsBatch(fsBatch);
        entrust.setBatchCount(entrust.getBatchCount() + 1);
        fsEntrustService.updateFsEntrust(entrust);
        return toAjax(1);
    }

    /**
     * 修改案件批次
     */
    @PreAuthorize("@ss.hasPermi('fs:batch:edit')")
    @Log(title = "案件批次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsBatch fsBatch) {
        return toAjax(fsBatchService.updateFsBatch(fsBatch));
    }

    /**
     * 删除案件批次
     */
    @PreAuthorize("@ss.hasPermi('fs:batch:remove')")
    @Log(title = "案件批次", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fsBatchService.deleteFsBatchByIds(ids));
    }
}
